<?php

namespace app\common;

use app\common\behavior\Init;
use app\common\model\ToolModel;
use think\Db;

class ConfigBase
{
    //获取公司去除禁用
    static public function getCompany()
    {
        return ToolModel::select('base_company',['status'=>1],'company_name','','id');
    }
    //获取全部公司
    static public function getCompanyAll($where=[])
    {
        return ToolModel::select('base_company',$where,'company_name','','id');
    }
    //获取国家 id=>名字
    static public function getCountry()
    {
        return ToolModel::select('base_country',[],'name_zh','','id');
    }
    //获取国家 名字=>国家
    static public function getCountryKeyName()
    {
        return ToolModel::select('base_country',[],'name_zh','','id');
    }
    //获取店铺
    static public function getShop()
    {
        return ToolModel::select('base_shop',['status'=>1],'shop_name','','id');
    }
    //获取全部店铺
    static public function getShopAll()
    {
        return ToolModel::select('base_shop',[],'shop_name','','id');
    }
    //获取店铺 名字做key
    static public function getShopKeyName()
    {
        return ToolModel::select('base_shop',['status'=>1],'id','','shop_name');
    }
    //获取仓库
    static public function getWarehouse()
    {
        return ToolModel::select('base_warehouse',['status'=>1],'warehouse_name','','id');
    }
    //获取全部仓库
    static public function getWarehouseAll($where=[])
    {
        return ToolModel::select('base_warehouse',$where,'warehouse_name','','id');
    }
    //获取店仓
    static public function getWarehouseShop()
    {
        return ToolModel::select('base_warehouse',['status'=>1,'type'=>1],'warehouse_name','','id');
    }
    //获取国家仓
    static public function getWarehouseCountry()
    {
        return ToolModel::select('base_warehouse',['status'=>1,'type'=>2],'warehouse_name','','id');
    }
    //获取国家仓或店仓的可用库存和加权单价
    static public function getinStockPrice()
    {
        $condition=[];$countryWarehouse=[];
        $shopWarehouse=[];$country=[];$shop=[];
        $warehouse=ToolModel::select('base_warehouse',[],'type','',"id");
        if($warehouse){
            foreach($warehouse as $key=>$value){
                if($value==1){
                     $shopWarehouse[]=$key;
                }else if($value==2){
                    $countryWarehouse[]=$key;
                }
            }
        }
        if($countryWarehouse){  //国家仓
            $condition['receipt_warehouse_id']=["in",$countryWarehouse];
            $country=ToolModel::select('warehouse_country_stock',$condition,'available_stock,wam','',"concat(receipt_warehouse_id,'_',sku)");
        }
        if($shopWarehouse){  //店仓
            $condition=[];
            $condition['base_warehouse_id']=["in",$shopWarehouse];
            $shop=ToolModel::select('shop_warehouse_stock',$condition,'physical_stock as available_stock,wam','',"concat(base_warehouse_id,'_',sku)");
        }
        $result=array_merge($country,$shop);
        return $result;
    }
    //获取用户国家仓数据权限
    static public function getWarehouseCountryPermission(){
        return explode(',',Init::$user['warehouse_country_permission']);
    }
    //获取用户店仓数据权限
    static public function getWarehouseShopPermission(){
        return Init::$user['warehouse_shop_permission'];
    }
    //获取店铺信息
    static public function getShopAllInfo(){
        return ToolModel::select('base_shop',[],'*','','id');
    }
    //获取计划任务最后一次成功时间-120秒
    static public function getTasksTime($type,$minus=120){
       $time=ToolModel::getValue('tasks',['type'=>$type],'date','id desc');
       if (empty($time)){
           return "2019-06-01 00:00:01";
       }
       return date("Y-m-d H:i:s",strtotime($time)-$minus);
    }
    static public function insertTasksTime($type){
        ToolModel::insertData(['type'=>$type,'date'=>TIME],'tasks');
    }
    //获取币种信息
    static public function getCurrencyAllInfo(){
        return Db::name('exchange_rate')->group('name')->order('id','asc')->column('id,name','id');
    }
    //获取部分公司  2：零售公司 3：贸易及零售公司
    static public function getCompanys()
    {
        return Db::name('base_company')
            ->where('status','=',1)
            ->where('type','in',[2,3])
            ->order('id','asc')
            ->column('id,company_name','id');
    }
    //获取出库总成本
    static public function getDeliveryTotalCost()
    {
        
        $result=Db::name('shop_warehouse_out a')
           ->join('base_warehouse w','a.base_warehouse_id = w.id','left')
           ->field('sum(price*quantity) totalCost,w.base_shop_id,concat(w.base_shop_id,"_",sku) as shopsku')
           ->where('w.type','=',1)
            ->where('a.type','=',2)
            ->group('concat(w.base_shop_id,"_",sku)')->select();
         if($result){
             return array_column($result,'totalCost','shopsku');
         }else{
             return [];
         }
    }
    //获取最新的币种信息exchangeRate
    static public function getExchangeRateInfo(){
        $buildSql=Db::table('exchange_rate')->field('*')->order('date','desc')->buildSql();
        $result=Db::table("$buildSql a")->group('a.name')->order('a.date','desc')->column('a.bankConversionPri','a.name');
        return $result;
    }
}
